package com.bowlofnoodles.truffleclient;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.google.android.c2dm.C2DMBaseReceiver;

public class C2DMReceiver extends C2DMBaseReceiver {
	
	public C2DMReceiver() {
		super("dummy@google.com");
	}
	
	public C2DMReceiver(String senderId) {
		this();
	}
	
	@Override
	public void onRegistered(Context context, String registrationId) 
		throws java.io.IOException {
		// send registration id to server
		Log.e("C2DM", "Registered");
		Log.e("C2DM", registrationId);
	}

	@Override
	protected void onMessage(Context context, Intent intent) {
		Log.e("C2DM", "Message: Fantastic!!!");
		// Extract the payload from the message
		Bundle extras = intent.getExtras();
		if (extras != null) {
			System.out.println(extras.get("gramsFound"));
			NotificationManager notman = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
			Notification note = new Notification(R.drawable.icon, getString(R.string.truffleSearchEvent), System.currentTimeMillis());
			note.flags |= Notification.FLAG_AUTO_CANCEL;
			PendingIntent mainViewIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainView.class), 0);
			
			note.setLatestEventInfo(getApplicationContext(), "見つけた！", "トリュフを "+(extras.get("gramsFound")==null?"1":extras.get("gramsFound"))+"g 見つけた", mainViewIntent);
			notman.notify(0, note);
		}
	}

	@Override
	public void onError(Context context, String errorId) {
		Log.e("C2DM", "Error: " + errorId);
	}
	

}
